package com.example.demo.controller;

import com.example.demo.entity.Contract;
import com.example.demo.entity.Customer;
import com.example.demo.entity.Designer;
import com.example.demo.service.CustomerService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("/qrcode/{text}")
    public String apply(@PathVariable(name = "text")String text,HttpSession session){
        session.setAttribute("text",text);
        return "page1";
    }

    @RequestMapping(value = "" , method = RequestMethod.GET)
    public String tuijian(){
        return "login";
    }


    @RequestMapping(value = "/dengru")
    public String denglu(String code,String state,HttpSession session){
        System.out.println("++++++++++++++++++++++"+state);
        Calendar cal = Calendar.getInstance();
        /*int month = cal.get(Calendar.MONTH) + 1;
        if(month!=5){
            return "405";
        }*/
        int hours = cal.get( Calendar.HOUR_OF_DAY );
        if(hours<9 ||hours>23){
            return "404";
        }
        session.setAttribute("text",state);
        String openid = customerService.dengru(code);

        if(openid==null || openid.equals("")){
            return "page11";
        }
        Customer customer = customerService.findbyopenId(openid);
        if(customer==null){
            session.setAttribute("openid",openid);
            return "apply";
        }
        session.setAttribute("customer",customer);
        session.setAttribute("mobile",customer.getMobile());
        switch (customer.getStatus()){
            case 1:
                return "lucky";
            case 2:
            case 3:
                return "result";
        }

        return "page11";
    }



    @RequestMapping("/getDesId")
    @ResponseBody
    public String getDesId(HttpSession session){
        String desId =(String) session.getAttribute("desId");
        return desId;
    }

    @RequestMapping(value = "/apply" , method = RequestMethod.POST)
    @ResponseBody
    public Integer apply(Customer customer, HttpSession session){
        String openid = (String) session.getAttribute("openid");
        String desId = (String) session.getAttribute("text");
        customer.setOpenId(openid);
        customer.setDesId(desId);
        Integer result = customerService.apply(customer,openid);
        session.setAttribute("mobile",customer.getMobile());
        /*session.removeAttribute("openid");*/
        //Integer result = 1;
        return result;
    }

    @RequestMapping(value = "/applyed" , method = RequestMethod.GET)
    public String applyed(){
        return "lucky";
    }

    @RequestMapping("/getUserId")
    @ResponseBody
    public Customer getUserId(HttpSession session){
        Customer customer = new Customer();
        try{
            String mobile = (session.getAttribute("mobile")).toString();
            customer = customerService.getCustomerByMobile(mobile);
        }catch (Exception e){
            System.out.println("空指针异常");
        }
        Integer userId=customer.getUserId();
        session.setAttribute("customer"+userId,customer);
        return customer;
    }

    @RequestMapping("/updateIsRaffled")
    @ResponseBody
    public Integer updateIsRaffled(Integer userId){
        return customerService.updateIsRaffled(userId);
    }

    @RequestMapping("/getCustomerByUserId")
    @ResponseBody
    public Map getCustomerByuserId(Integer userId){
        Map map = new HashMap();
        Customer customer = customerService.getCustomerByuserId(userId);
        if (customer==null){
            map.put("msg",0);//该客户不存在
        } else {
            map.put("msg",1);
            map.put("customer",customer);
        }
        return map;
    }

    @RequestMapping("/getCustomerByMobile")
    @ResponseBody
    public Map getCustomerByMobile(String mobile){
        Map map = new HashMap();
        Customer customer = customerService.getCustomerByMobile(mobile);
        if (customer==null){
            map.put("msg",0);//该客户不存在
        } else {
            map.put("msg",1);
            map.put("customer",customer);
        }
        return map;
    }

    @RequestMapping("/result")
    public String result(){
        return "result";
    }

    @RequestMapping(value = "/getCustomerByDesId",method = RequestMethod.GET)
    public String getCustomerByDesId(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "mycustomer";
    }

    @RequestMapping(value = "/customerList",method = RequestMethod.POST)
    @ResponseBody
    public Map customerList(HttpSession session){
        Map map = new HashMap();
        Designer designer =(Designer) (session.getAttribute("designer"));
        String desId = designer.getDesId();
        List<Customer> customerList = customerService.getCustomerByDesId(desId);
        if(customerList==null || customerList.size()==0){
            map.put("msg",0);
        } else {
            map.put("msg",1);
            map.put("customerList",customerList);
        }
        return map;
    }

    @RequestMapping(value = "customerAdd",method = RequestMethod.GET)
    private String contractAdd(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "mycustomerAdd";
    }
    @RequestMapping(value = "/myAdministration",method = RequestMethod.GET)
    public String myAdministration(HttpSession session){
        Designer designer =(Designer)(session.getAttribute("designer"));
        if (designer==null) {
            return "login";
        }
        return "myAdministration";
    }

    @RequestMapping("/mysearch")
    @ResponseBody
    public List<Customer> mysearch(String keyword,HttpSession session){
        Designer designer =(Designer)(session.getAttribute("designer"));
        List<Customer> customertList = customerService.getCustomer(keyword,designer.getDesId());
        return customertList;
    }
}
